package com.zsj.hello.six;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.zsj.hello.util.RabbimqFactoryUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author https://gitee.com/zhengshengjun
 * @date 2023/8/31.
 */
public class Consumer_three {

    public static boolean autoAck = false;

    static int count = 0;

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbimqFactoryUtil.getRabbitMQChannel();
        //接收消息并且声明不公平队列
        channel.basicQos(1);
        DeliverCallback deliverCallback = (t, m) -> {
            System.out.println("消息的tag值为" + t);
            System.out.println("c3接收到的消息为" + new String(m.getBody(), StandardCharsets.UTF_8));
            //消息应答
            channel.basicAck(m.getEnvelope().getDeliveryTag(), false);
            count++;
        };

        CancelCallback cancelCallback = (T) -> {
            System.out.println("消息被取消了");
        };

        //设置不自动应答
        channel.basicConsume(Task04.QUEUE_NAME, autoAck, deliverCallback, cancelCallback);

        System.out.println("处理了"+count+"次消息");
    }
}
